﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace gisgeb.comserver
{
    [XmlRoot("gisgeb.config")]
    public class Config
    {
        public Config()
        {
            this.IgnoredPluginDllNames = new List<string>();
        }
        
        [XmlArrayAttribute("IgnoredPlugins")]
        [XmlArrayItem("Dll")]
        public List<string> IgnoredPluginDllNames { get; set; }
        
        public static Config LoadFromFolder(string installFolder)
        {
            string configFile = Path.Combine(installFolder, "config.xml");
            if(!File.Exists(configFile))
            {
                return new Config();
            }
            
            using(TextReader textReader = new StreamReader(configFile))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(Config));
                try
                {
                    return (Config)deserializer.Deserialize(textReader);
                }
                catch (InvalidOperationException e)
                {
                    throw new ConfigurationFileException(e.InnerException);
                }
                
            }
        }
        
        public void SaveTo(string fileName)
        {            
            using(TextWriter textWriter = new StreamWriter(fileName))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Config));
                serializer.Serialize(textWriter, this);                
            }
        }
    }
}
